//
//  SelectMyPhoto.m
//  Bespoke
//
//  Created by Cheng on 12/9/6.
//
//

#import "SelectMyPhoto.h"

@implementation SelectMyPhoto
@synthesize VC_Controller, Array_AccountInfo, BK_En_object;
@synthesize delegate;

BOOL bOpenVideo = YES;

- (void)GetController:(UIViewController *)view UserInfo:(NSMutableArray *)GetInfo {
    self.Array_AccountInfo = GetInfo;
    self.VC_Controller = view;
}

- (IBAction)UseCamera:(id)sender {
    
    UIImagePickerController *imagePickController =[[UIImagePickerController alloc]init];
    
    //You can use isSourceTypeAvailable to check
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickController.delegate=self;
        imagePickController.allowsEditing=NO;
        imagePickController.showsCameraControls=YES;
        
        
        //Enable Video.
        /**if (bOpenVideo) {
            NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:imagePickController.sourceType];
            imagePickController.mediaTypes = mediaTypes;
            [imagePickController setVideoQuality:UIImagePickerControllerQualityTypeMedium];  //Set video quality
            [imagePickController setVideoMaximumDuration:960];  //Set Maximum Video time.
        }**/
    }
    else {
        [self SetModal:VC_Controller PhotoSource:nil PhotoName:nil PhotoType:nil];
    }
    [VC_Controller presentModalViewController:imagePickController animated:YES];
}

- (IBAction)UsePhotoLibrary:(id)sender {
    [self SetModal:VC_Controller PhotoSource:nil PhotoName:nil PhotoType:nil];
}


- (IBAction)CancelMyPhoto:(id)sender {
    [delegate uiViewTouched_SelectMyPhoto:NO];
}

- (void)SetModal:(UIViewController *)view PhotoSource:(UIImage *)SetPhoto PhotoName:(NSString *)SetPhotoName PhotoType:(NSString *)SetPhotoType {
    
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    MyPhotoViewController *VC_MyPhoto = [storyboard instantiateViewControllerWithIdentifier:@"MyPhotoViewController"];
    
    VC_MyPhoto.delegate = self;
    
    VC_MyPhoto.Image_MyPhoto = SetPhoto;
    VC_MyPhoto.Array_AccountInfo = Array_AccountInfo;
    VC_MyPhoto.NS_MyPhotoName = SetPhotoName;
    VC_MyPhoto.NS_PhotoType = SetPhotoType;
    VC_MyPhoto.BK_En_object = self.BK_En_object;
    
    [view presentModalViewController:VC_MyPhoto animated:YES];
}


#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    //Check source is image.
    if ([mediaType isEqualToString:@"public.image"]) {
        UIImage *Image_Photo = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        
        UIImageWriteToSavedPhotosAlbum(Image_Photo, nil, nil, nil);//Save Photo to library.
        
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
        
        [self SetModal:picker PhotoSource:Image_Photo PhotoName:@"camera.jpg" PhotoType:@"JPG"];
    }
}

#pragma mark - MyPhotoViewControllerDelegate

- (void)MyPhotoViewControllerDidClose:(MyPhotoViewController *)controller {
    [delegate uiViewTouched_SelectMyPhoto:NO];
    [VC_Controller dismissViewControllerAnimated:NO completion:nil];
}

- (void)MyPhotoViewControllerDidSave:(MyPhotoViewController *)controller BoardName:(NSString *)GetBoardName {
    [delegate PhotoSaveSuccess:GetBoardName];
    [VC_Controller dismissViewControllerAnimated:NO completion:nil];
}

@end
